package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.remote.tencent.api.TecentMapFeign;
import com.power.sifa.correction.api.ICrc_cameraFeign;
import com.power.sifa.correction.model.Crc_camera;
import com.power.sifa.correction.model.pageInfo.Crc_cameraPageInfo;
import com.power.sifa.correction.service.Crc_cameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   摄像头信息控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Crc_cameraController",tags={"摄像头信息"})
public class Crc_cameraController implements ICrc_cameraFeign {

    @Resource
    private Crc_cameraService crc_cameraService;

    @Resource
    private TecentMapFeign tecentMapFeign;


   @Value("${tencent.map.key:#{null}}")
   private String key;


    @Override
    @ApiOperation("查询摄像头信息分页列表")
    @SaCheckPermission("crc_camera_list")
    public ResponseDTO list(Crc_cameraPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Crc_camera> page = this.crc_cameraService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Crc_camera crc_camera : page) {
                crc_camera.geneText();
                list.add(crc_camera);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看摄像头信息")
    @SaCheckPermission("crc_camera_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Crc_camera crc_camera  = crc_cameraService.getById(id);
        if (crc_camera != null) {
            crc_camera.geneText();
        }
        return ResponseDTO.succData(crc_camera);
    }

    @Override
    @ApiOperation("删除摄像头信息")
    @SaCheckPermission("crc_camera_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        crc_cameraService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改摄像头信息")
    @SaCheckPermission("crc_camera_save")
    public ResponseDTO save(Crc_camera crc_camera) {
        Assert.notNull(crc_camera, "crc_camera不能为空");
        crc_cameraService.saveOrUpdate(crc_camera);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除摄像头信息")
    @SaCheckPermission("crc_camera_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<String> list = Arrays.asList(ids.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
        crc_cameraService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("crc_camera_list")
    public void output(HttpServletResponse response, Crc_cameraPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "摄像头信息";
        String[] heads = {"序号","名称","纬度","经度","删除标记"};
        String[] fields = {"id","name","lat","lng","deleted"};
        Page<Crc_camera> page = this.crc_cameraService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "摄像头信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","名称","纬度","经度","删除标记"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","lat","lng","deleted"};
        List<Crc_camera> list = (List<Crc_camera>)ExcelUtil.Import(fields, file, Crc_camera.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.crc_cameraService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO reverseParseAddress(String location) {
        return ResponseDTO.succData(tecentMapFeign.reverseParseAddress(this.key, location));
    }

    @Override
    public ResponseDTO getCameraSize() {
        Integer size = crc_cameraService.getCameraSize();
        log.info("摄像头数量:{}", size);
//        return ResponseDTO.succ();
        return ResponseDTO.succData(size);
    }


}
